<?php
/**[内容视图模型]
 * @Author: 976123967@qq.com
 * @Date:   2014-08-15 18:14:52
 * @Last Modified by:   chen
 * @Last Modified time: 2014-10-28 22:28:49
 */

class ArticleFlagViewModel extends ViewModel{
	protected $tableName='article';

	public $viewFields  = array(
		'article'=>array(
			'*',
			'_type'=>'INNER',
		),
		'user'=>array(
			'username','uid',
			'_type'=>'INNER',
			'_on' =>'user.uid=article.user_uid',
		),
		'category'=>array(
			'cid','cname','remark',
			'_type'=>'INNER',
			'_on' =>'category.cid=article.category_cid',
		)
	);


	/**
	 * [get_flag 属性文档]
	 * @param  [type] $cid  [description]
	 * @param  [type] $flag [description]
	 * @param  [type] $row  [description]
	 * @param  [type] $order  [description]
	 * @param  [type] $field  [description]
	 * @return [type]       [description]
	 */
	public function get_flag($cid,$flag,$row,$order='',$field=null)
	{


		$mid = D('Category')->get_field($cid,'model_mid');
		$table = 'article_'.D('Model')->get_field($mid,'name');
		$extField = D('Field')->get_lists_field($mid);

		/*$this->viewFields[$table] = array(
			'*',
			'_type'=>'INNER',
			'_on'=>$table.'.article_aid=article.aid'

		);*/




		$where['verifystate'] = 2;

		$categoryModel = D('Category');
		if($cid)
		{
			// 获取所有的子cid
			$childCid = $categoryModel->get_child_cid($cid);
			$where['category_cid'] = array('in',$childCid) ;
		}

		if($flag)
		{
			// 解析成正确的值
			$infoFlag = C('flag');

			$flag = isset($infoFlag[$flag])?$infoFlag[$flag]:'';
			if($flag)
				$where[] = "find_in_set('{$flag}',flag)";			
		}


		// 排序
		if(!$order)
			$order = 'article.sort asc ,aid desc';

		if(is_null($field))
			$field='sort,verifystate,aid,category_cid,article_title,keywords,description,username,cname,remark,edittime,addtime,pic,flag,article_title_en,keywords_en,description_en';
		if($categoryModel->get_field($cid,'cat_type')==4)
			$field .=  ',content';
		if($flag=='图文')
			$field .=  ',pic';
		$data = $this->field($field)->where($where)->order($order)->limit($row)->select();
		if(!$data) return $data;
		$db =  M($table);
		foreach($data as $k=>$v)
		{
			// 编辑时间
			$data[$k]['edittime']=date('Y-m-d',$v['edittime']);
			// 发布时间
			$data[$k]['addtime']=date('Y-m-d',$v['addtime']);
			// 图片
			$data[$k]['pic']= $v['pic']?__ROOT__.'/'.$v['pic']:__ROOT__.'/Data/Image/default.gif';

			// 扩展字段
			if($extField)
			{
				$temp = $db->field($extField)->where(array('article_aid'=>$v['aid']))->find();
				$data[$k] = array_merge($data[$k],$temp);
			}
			
		}



		return $data;

	}

	/**
	 * [next 上一条和下一条]
	 * @param  [type] $aid [description]
	 * @return [type]      [description]
	 */
	public function next($flag=1,$lang='cn')
	{

		$aid =  I('get.aid',null,'intval');
		$cid = I('get.cid',null,'intval');



		// 下一条
		$fieldNext = $this->where('aid<'.$aid.' and category_cid='.$cid)->order('aid desc')->find();
		if($fieldNext)
			if($lang=='cn')
				$next = "<a href='".U('Home/'.$fieldNext['remark'].'/view',array('cid'=>$fieldNext['category_cid'],'aid'=>$fieldNext['aid']))."'>{$fieldNext['article_title']}</a>";
			else
				$next = "<a href='".U('Home/'.$fieldNext['remark'].'/view',array('cid'=>$fieldNext['category_cid'],'aid'=>$fieldNext['aid']))."'>{$fieldNext['article_title_en']}</a>";
		else
		{
			if($lang=='cn')
				$next = '没有了';
			else
				$next = 'none';
		}

		
		// 上一条
		$fieldPre = $this->where('aid>'.$aid.' and category_cid='.$cid)->order('aid asc')->find();
		if($fieldPre)
			if($lang=='cn')
				$pre = "<a href='".U('Home/'.$fieldPre['remark'].'/view',array('cid'=>$fieldPre['category_cid'],'aid'=>$fieldPre['aid']))."'>{$fieldPre['article_title']}</a>";
			else
				$pre = "<a href='".U('Home/'.$fieldPre['remark'].'/view',array('cid'=>$fieldPre['category_cid'],'aid'=>$fieldPre['aid']))."'>{$fieldPre['article_title_en']}</a>";
		else
			if($lang=='cn')
				$pre = '没有了';
			else
				$pre = 'none';

		switch ($flag) 
		{
			// 上一条和下一条
			case 1:
				return $pre.$next;
			
			// 上一条
			case 2:
				return $pre;
			
			//下一条
			case 3:
				return $next;
				
		}


	}
}